#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <unistd.h>

#include <errno.h>

#include <sys/socket.h>
#include <sys/epoll.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#define MAXQUEUE			10
#define EPOLL_SIZE			1000

static int setmulticast(void)
{
	int ret = 0;
	char devname[64] = {0};
	unsigned long d = 0;

	FILE *fp = fopen("/proc/net/route", "r");
	
	if(NULL != fp)
	{
		ret = fscanf(fp, "%*[^\n]\n");
		if(0 < ret)
		{
			ret = fscanf(fp, "%63s%lx%*[^\n]\n", devname, &d);
			if(2 == ret)
			{
				printf("%s\n", devname);
			}
		}
		fclose(fp);
	}

	return 0;
}

static int setnonblocking(int fd)
{
	int opts = fcntl(fd, F_GETFL);

	if(0 > opts)
	{
		return -1;
	}

	if(0 > fcntl(fd, F_SETFL, opts | O_NONBLOCK))
	{
		return -1;
	}

	return 0;
}

static int init_listen(int *psock, int port)
{
	int ret = 0;

	int fd = 0;
	struct sockaddr_in listen_addr;

	const int opt = 1;
	
	fd = socket(AF_INET, SOCK_STREAM, 0);
	
	if(0 > fd)
	{
		goto out;
	}

	ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

	if(0 != ret)
	{
		goto out;
	}
	
	ret = setnonblocking(fd);

	if(0 != ret)
	{
		goto out;
	}

	memset(&listen_addr, 0, sizeof(listen_addr));
	listen_addr.sin_family = AF_INET;
	listen_addr.sin_port = htons(port);
	listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);

	ret = bind(fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr));

	if(0 != ret)
	{
		goto out;
	}
	
	ret = listen(fd, MAXQUEUE);

	if(0 != ret)
	{
		goto out;
	}
	
	*psock = fd;

out:
	return ret;
}

static int init_multicase(int *psock, struct sockaddr_in *multi_addr, char *ip, int port)
{
	int ret = 0;

	int fd = 0;

	const int opt = 1;
	
	setmulticast();

	fd = socket(AF_INET, SOCK_DGRAM, 0);

	if(0 > fd)
	{
		goto out;
	}

	ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

	if(0 != ret)
	{
		goto out;
	}

	memset(multi_addr, 0, sizeof(*multi_addr));
	multi_addr->sin_family = AF_INET;
	multi_addr->sin_port = htons(port);
	multi_addr->sin_addr.s_addr = inet_addr(ip);
	
	system("route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0");

	*psock = fd;

out:
	return ret;
}

int main(int argc, char **argv)
{
	int ret = 0;
	int i = 0;

	int socket_listen = -1, socket_group = -1, socket_client = -1;

	int logfd = 0;

	struct sockaddr_in multi_addr, client_addr;
	socklen_t client_len = sizeof(client_addr);

	int epoll_fd = 0, epoll_events_count = 0;
	struct epoll_event ev, events[EPOLL_SIZE];

	char buffer[1024] = {0};

	logfd = open("/dev/logman", O_RDWR | O_NONBLOCK);

	if(0 <= logfd)
	{
		if(0 == init_listen(&socket_listen, 65530) && 0 <= socket_listen)
		{
			if(0 == init_multicase(&socket_group, &multi_addr, (char *)"224.1.1.1", 65530) && 0 <= socket_group)
			{
				epoll_fd = epoll_create(EPOLL_SIZE);

				if(0 <= epoll_fd)
				{
					ev.events = EPOLLIN | EPOLLET;
					ev.data.fd = logfd;

					if(0 == epoll_ctl(epoll_fd, EPOLL_CTL_ADD, logfd, &ev))
					{
						ev.events = EPOLLIN | EPOLLET;
						ev.data.fd = socket_listen;
					
						if(0 == epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_listen, &ev))
						{
							while(1)
							{
								memset(buffer, 0, sizeof(buffer));

								epoll_events_count = epoll_wait(epoll_fd, events, EPOLL_SIZE, 3000);

								if(0 < epoll_events_count)
								{
									for(i = 0; i < epoll_events_count; i++)
									{
										if(events[i].data.fd == socket_listen)
										{
											socket_client = accept(socket_listen, (struct sockaddr *)&client_addr, &client_len);

											if(0 <= socket_client)
											{
												if(0 == setnonblocking(socket_client))
												{
													ev.events = EPOLLIN | EPOLLET;
													ev.data.fd = socket_client;

													if(0 == epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_client, &ev))
													{
														printf("New client connected, client ip is %s, port is %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
														continue;
													}
												}
												close(socket_client);
											}
											socket_client = -1;
										}
										else if(events[i].data.fd == logfd)
										{
											do
											{
												memset(buffer, 0, sizeof(buffer));
												ret = read(logfd, buffer, sizeof(buffer));
												if(0 < ret)
												{
													sendto(socket_group, buffer, ret, 0, (struct sockaddr*)&multi_addr, sizeof(multi_addr));
												}
											} while(0 < ret);
										}
										else if(0 != (events[i].events & EPOLLIN))
										{
											socket_client = events[i].data.fd;

											if(0 < read(socket_client, buffer, sizeof(buffer)))
											{
												
											}
											else
											{
												epoll_ctl(epoll_fd, EPOLL_CTL_DEL, socket_client, NULL);
												close(socket_client);
												socket_client = -1;
											}
										}
									}
								}
								else if(0 > epoll_events_count)
								{
									break;
								}
							}
							epoll_ctl(epoll_fd, EPOLL_CTL_DEL, socket_listen, NULL);
						}
						epoll_ctl(epoll_fd, EPOLL_CTL_DEL, logfd, NULL);
					}
					close(epoll_fd);
				}
				close(socket_group);
			}
			close(socket_listen);
		}
		close(logfd);
	}
	else
	{
		perror("open");
	}

	return 0;
}